package com.example.soa_exp2.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.soa_exp2.Domain.Student;
import com.example.soa_exp2.Mapper.StudentMapper;
import com.example.soa_exp2.Service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private Student student;

    @Resource
    private StudentMapper studentMapper;

    @Override
    public List<Student> getStudentList(){
        return studentMapper.selectList(null);
    }

    @Override
    public Student getStudentByNum(String num){
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.eq("num", num);
        return studentMapper.selectOne(wrapper);
    }

    @Override
    public List<Student> getStudentByName(String name){
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return studentMapper.selectList(wrapper);
    }

    @Override
    public int addStudent(Student student) {
        List<Student> students = getStudentList();
        for (Student student_a: students) {
            if(Objects.equals(student_a.getNum(), student.getNum())){
                return 0;
            }
        }
        return studentMapper.insert(student);
    }

    @Override
    public int deleteStudentById(String stu_num){
        return studentMapper.deleteById(stu_num);
    }

    @Override
    public int updateStudentById(Student student){
        return studentMapper.updateById(student);
    }
}
